/**
 * Created by Arnald on 2017/6/10.
 */
const md5 = require('md5');
const userDao = require('../mysql/userDao.js');
const advanceDao = require('../mysql/advanceDao.js');
const variable = require('../config/variable.js');

var create = module.exports;

create.auto = function (cb) {
    advanceDao.autoAccount(function (err, res) {
        var account = 'auto' + res;
        var password = parseInt(Math.random() * 89999999 + 10000000).toString();
        create.user(account, password, function (err, res, token) {
            cb(err, res, {'account': account, 'password': password, 'token': token});
        });
    });
};
create.user = function (account, password, cb) {
    var token = create.token();

    var userModel = new userDao();
    userModel.set('account', account);
    userModel.set('name', account);
    userModel.set('password', md5(password));
    userModel.set('gold', variable.config.regGold);
    userModel.set('card', gameData.d.config.regCard);
    userModel.set('token', token);
    userModel.save(function (err, res) {
        cb(err, res, token);
    });
};
create.blued = function (uid, name, cb) {
    var token = create.token();

    var userModel = new userDao();
    userModel.set('bluedId', uid);
    userModel.set('account', 'blued_' + uid);
    userModel.set('name', name);
    userModel.set('password', md5('blued_' + uid + '_password'));
    userModel.set('gold', variable.config.regGold);
    userModel.set('card', gameData.d.config.regCard);
    userModel.set('token', token);
    userModel.save(function (err, res) {
        cb(err, res, token);
    });
};

create.lovelive = function (uid,groupid,name,avatar, cb) {
    let token = create.token(); 
    let userModel = new userDao(); 
    userModel.set('groupId', groupid);
    userModel.set('account', uid); 
    if (name && name != null) {
        userModel.set('name', name);
    }   
    if (avatar && avatar != null) {
        userModel.set('avatar', avatar);
    }   
    userModel.set('password', md5(uid));
    userModel.set('gold', gameData.d.config.regGold);
    userModel.set('card', gameData.d.config.regCard);
    userModel.set('token', token);
    userModel.save(function (err, res) {
        if (res == null) {
            cb(err, res, token);
        }   
        let date = new Date().Format("yyyy-MM-dd hh:mm:ss");
        let regModel = new regDao();
        regModel.set('userId', res.res.insertId);
        regModel.set('date', date);
        regModel.save(function () {
        });
        cb(err, res, token);
    });
};

create.reToken = function (userModel, cb) {
    var random = parseInt(100000 + (999999 - 100000) * (Math.random()));
    var token = md5(userModel.data.id + '&' + random);
    userModel.set('token', token);
    userModel.save(function (err, res) {
        if (err != null) {
            cb(err, null);
            return false;
        }
        cb(null, {'token': token});
        return false;
    });
};
create.reAvatar = function (userModel,avatar) {
    userModel.set('avatar', avatar);
    userModel.save(function (err, res) {
        if (err != null) {
            return false;
        }
        return false;
    });
};




create.token = function () {
    var random = parseInt(100000 + (999999 - 100000) * (Math.random()));
    var token = md5(Date.now() + '&' + random);
    return token;
};
